package com.lobstar.xrpc.example;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.type.TypeReference;
import com.lobstar.xrpc.client.main.MainClient;
import com.lobstar.xrpc.example.bo.AUser;
import com.lobstar.xrpc.example.bo.BUser;
import com.lobstar.xrpc.example.services.B;
import com.lobstar.xrpc.server.handler.task.ReturnHandler;
import com.lobstar.xrpc.tools.Fields;

public class SimpleClient {

	public static void main(String[] args) throws Exception {
		//启动
		MainClient client = MainClient.build("1", "localhost", 20002,"").blockStart();
		//同步调用
		Integer call = client.asyncCall("sample_add", Fields.build().field("a", 5).field("b", 7), Integer.class);
		System.out.println(call);
		//异步调用
		client.syncCall("sample_add", Fields.build().field("a", 18).field("b", 22), Integer.class,
				new ReturnHandler<Integer>() {
					@Override
					public void handle(Integer t) {
						System.out.println("scall->" + t);
					}

					@Override
					public void exception(String exMsg) {
						System.out.println("e_exception->" + exMsg);
					}
				});
		client.syncCall("sample_add", Fields.build().field("a", 17).field("b", 22), Integer.class,
				new ReturnHandler<Integer>() {
					@Override
					public void handle(Integer t) {
						System.out.println("scall->" + t);
					}

					@Override
					public void exception(String exMsg) {
						System.out.println("e_exception->" + exMsg);
					}
				});
		System.out.println("did sample add sync");
		//异步复杂返回值调用
		Date date = client.asyncCall("format", Fields.build().field("date", "2011-02-12"), Date.class);
		System.out.println(date);
		List<String> list = client.asyncCall("list", Fields.build(), List.class);
		System.out.println(list);

		B b = client.asyncCall("getA", Fields.build().field("a1", 3).field("a2", "xx"), B.class);
		System.out.println(b);

		List<B> bs = client.asyncCall("alist", Fields.build(), List.class);
		System.out.println(bs);

		int plus = client.asyncCall("plus", Fields.build().field("a", 3).field("b", 2), Integer.class);
		System.out.println(plus);

		try {
			double divide = client.asyncCall("divide", Fields.build().field("a", 9).field("b", 0), Double.class);
			System.out.println(divide);
		}catch(Exception e) {
			System.out.println(e);
		}
		AUser user = new AUser();
		user.setName("xxx");
		user.setContent("conetnt");
		user.setAge(12);
		user.setSex(true);
		BUser ret = client.asyncCall("add_user", Fields.build().field("user", user), BUser.class);
		System.out.println(ret.getAge());
		Thread.sleep(12000);
		client.close();
	}
}
